package com.yz.jdd.module.psd.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.base.activity.BaseActivity;
import com.yz.jdd.base.event.Events;
import com.yz.jdd.base.event.RxBus;
import com.yz.jdd.base.utils.EditTextUtils;
import com.yz.jdd.module.login.view.LoginActivity;
import com.yz.jdd.module.psd.presenter.ModifyPsdPresenter;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by cc on 2017/5/23.
 */

public class ModifyPsdActivity extends BaseActivity implements ModifyPsdView {
    @BindView(R.id.top_back)
    ImageView topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.tel_no)
    EditText telNo;
    @BindView(R.id.verify_code)
    EditText verifyCode;

    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    ModifyPsdPresenter presenter;
    String phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modifypsd);
        ButterKnife.bind(this);
        presenter = new ModifyPsdPresenter(this);
        phoneNum = getIntent().getStringExtra("forget");
        initView();
    }

    private void initView() {
        topTitle.setText("修改密码");
        topBack.setVisibility(View.VISIBLE);
        topRight.setVisibility(View.VISIBLE);
        topRight.setText("完成");
        topRight.setVisibility(View.GONE);
        EditTextUtils.inputTextCheck(btnConfirm, Arrays.<TextView>asList(telNo,verifyCode));
    }

    @OnClick({R.id.top_back, R.id.top_right, R.id.btn_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_back:
                finish();
                break;
            case R.id.top_right:
                modifyPsd();
                break;
            case R.id.btn_confirm:
                modifyPsd();
                break;
        }
    }

    private void modifyPsd() {
        String psd1 = telNo.getText().toString();
        String psd2 = verifyCode.getText().toString();
        if (TextUtils.isEmpty(psd1)) {
            showToastDialog("请输入新密码");
        } else if (TextUtils.isEmpty(psd2)) {
            showToastDialog("请再次输入新密码");
        } else if (!TextUtils.isEmpty(psd1) && !TextUtils.isEmpty(psd2)) {
            if (!psd1.equals(psd2)) {
                showToastDialog("您两次输入的密码不一致");
            } else {
                String psdPattern = "[a-zA-z0-9~!@#$%^&*()_+-={}|\\\\[\\\\]\\\\:\\\";'<>?,./]{6,14}";
                Pattern pattern = Pattern.compile(psdPattern);
                Matcher matcher = pattern.matcher(psd2);


                if (matcher.matches()) {
//                            if(!TextUtils.isEmpty(forget)){
//                                //重置密码
                    presenter.resetPwd(phoneNum, psd2);
//
//                            }else{
//                                presenter.modifyPsd(psd2);
//                            }
                } else {
                    showToastDialog("您输入的密码格式有误");
                }
            }
        }
    }

    @Override
    public void startLoginActivity() {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @Override
    public void colseActivity(String phoneNo) {
        //修改密码成功，需要关掉验证手机界面
        RxBus.getInstance().send(Events.EVENT_MODFIFYPSD_SUCCESS, null);
        finish();
    }
}
